import os
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QPushButton
import globalvar
# 引用module_strategy.py 策略过滤体规则
import module_strategy

# 所有过滤规则
list_strategyrule =['无']

def Function_EditStrategyFilter(self):
    button = globalvar.ui.MainWindow.sender()
    if button:
        row = globalvar.ui.table_strategyfilter.indexAt(button.pos()).row()
    os.system(globalvar.ui.table_strategyfilter.item(row, 1).text())

# strategyfilterfile目录下策略过滤条件，添加到主视图table控件列表
def Function_ScanStrategyFilter():
    path = "strategyfilterfile"
    ls = os.listdir(path)
    for i in ls:
        c_path = os.path.join(path, i)
        if os.path.isdir(c_path):
            globalvar.ui.ClearPath(c_path,".txt")
        else:
            file = os.path.splitext(c_path)
            filename, type = file
            if type == ".py" or type == ".pyd" or type == ".pyc":
                filename2 = filename.replace('strategyfilterfile\\', '')
                if filename2 in module_strategy.dict_strategyopenrule1:
                    pass
                else:
                    try:
                        list_strategyrule.append(c_path)
                        row_cnt = globalvar.ui.table_strategyfilter.rowCount()  # 返回当前行数（尾部）
                        globalvar.ui.table_strategyfilter.insertRow(row_cnt)  # 尾部插入一行新行表格
                        item = QTableWidgetItem(str(row_cnt + 1))
                        globalvar.ui.table_strategyfilter.setItem(row_cnt, 0, item)
                        item = QTableWidgetItem(c_path)
                        globalvar.ui.table_strategyfilter.setItem(row_cnt, 1, item)
                        globalvar.ui.table_strategyfilter.setItem(row_cnt, 2, item)
                        globalvar.ui.table_strategyfilter.setItem(row_cnt, 3, item)
                        deleteButton = QPushButton("编辑策略过滤规则")
                        globalvar.ui.table_strategyfilter.setCellWidget(row_cnt, 3, deleteButton)
                        deleteButton.clicked.connect(Function_EditStrategyFilter)
                    except Exception as e:
                        print("Function_ScanStrategyFilter Error:" + repr(e))
